package com.sds.ai.runner.ui;


import com.google.common.collect.Lists;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;


public class ChatWindows extends JFrame {

    private JTextArea chatLog;
    private JTextField chatMessage;
    private JButton sendButton;
    private List<String> oldMessages = Lists.newArrayList();
    private int index = 0;

    public ChatWindows() throws HeadlessException {
        super("Kim the chatter bot");

        chatLog = new JTextArea();
        chatLog.setEditable(false);
        chatLog.setLineWrap(true);
        chatLog.setWrapStyleWord(true);

        chatMessage = new JTextField();

        sendButton = new JButton("Send");

        Panel bottomPanel = new Panel();
        bottomPanel.setLayout(new BorderLayout());
        bottomPanel.add(chatMessage, BorderLayout.CENTER);
        bottomPanel.add(sendButton, BorderLayout.EAST);

        JScrollPane centerScrollPanel = new JScrollPane(chatLog);
        centerScrollPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        this.setLayout(new BorderLayout());
        this.add(centerScrollPanel, BorderLayout.CENTER);
        this.add(bottomPanel, BorderLayout.SOUTH);
        this.setSize(800, 800);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
        addHandler();

        centerScreen();
    }

    @Override
    public void setVisible(boolean b) {
        super.setVisible(b);
        chatMessage.requestFocus();
    }

    private void addHandler() {
        chatMessage.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
            }

            @Override
            public void keyPressed(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    sendChatMessage(chatMessage.getText());
                } else if (e.getKeyCode() == KeyEvent.VK_UP) {
                    moveUpOldMessages();
                } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                    moveDownOldMessages();
                }
            }
        });


        sendButton.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                sendChatMessage(chatMessage.getText());
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    }

    private void moveDownOldMessages() {
        if (index < oldMessages.size() - 1) {
            index++;
        }
        chatMessage.setText(oldMessages.get(index));
    }

    private void moveUpOldMessages() {
        if (index > 0) {
            index--;
        }
        chatMessage.setText(oldMessages.get(index));
    }

    private void sendChatMessage(String message) {
        chatLog.setText(chatLog.getText() + "YOU : " + message + "\n");
        chatLog.setCaretPosition(chatLog.getText().length());

        chatMessage.setText("");

        if (oldMessages.size() > 100) {
            oldMessages.remove(0);
        }
        index = oldMessages.size();
        oldMessages.add(message);

        //TODO: remove this locs
        responseChatMessage("you said " + message.toUpperCase());
    }

    private void responseChatMessage(String message) {
        chatLog.setText(chatLog.getText() + "KIM : " + message + "\n");
    }

    private void centerScreen() {
        // Get the size of the screen
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        // Determine the new location of the window
        int w = this.getSize().width;
        int h = this.getSize().height;
        int x = (dim.width - w) / 2;
        int y = (dim.height - h) / 2;

        // Move the window
        this.setLocation(x, y);
    }

}
